﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/
        TuanAnhEntities _db = new TuanAnhEntities();

        public ActionResult Menu()
        {
            IEnumerable<TUANANH__POSITION> menu = _db.TUANANH__POSITION;
            return View(menu);
        }
         [HandleError(View = "NoEMP")]
        public ActionResult Index(  int posId = 1)
        {
            Room r = new Room();
            if (posId == 1)
            {
                var employees = (from e in _db.TUANANH__EMPLOYEE
                                join p in _db.TUANANH__POSITION on e.POS_ID equals p.POS_ID
                                where e.EMP_ID == p.LEADER_ID
                                select new StaffInfo
                                           {
                                              EmpId = e.EMP_ID,
                                              EmpName = e.EMP_NAME, 
                                              EmpAge = e.EMP_AGE,
                                              EmpInfo = e.EMP_INFO,
                                              EmpImage = e.EMP_IMAGE,
                                              EmpTitle = e.EMP_TITLE,
                                              PosId = e.POS_ID,
                                              PosDes = p.POS_DES,
                                              LeaderId = p.LEADER_ID
                                           }).ToList();

                r.Leader = employees.SingleOrDefault(x => x.PosId == posId);
                r.Subordinates = employees.Where(x => x.PosId != posId);
            }

            else
            {
                var employees = (from e in _db.TUANANH__EMPLOYEE
                                 join p in _db.TUANANH__POSITION on e.POS_ID equals p.POS_ID
                                 where e.POS_ID == posId
                                 select new StaffInfo
                                 {
                                     EmpId = e.EMP_ID,
                                     EmpName = e.EMP_NAME,
                                     EmpAge = e.EMP_AGE,
                                     EmpInfo = e.EMP_INFO,
                                     EmpImage = e.EMP_IMAGE,
                                     EmpTitle = e.EMP_TITLE,
                                     PosId = e.POS_ID,
                                     PosDes = p.POS_DES,
                                     LeaderId = p.LEADER_ID
                                 }).ToList();
                r.Leader = employees.SingleOrDefault(x => x.EmpId == x.LeaderId);
                r.Subordinates = employees.Where(x => x.EmpId != x.LeaderId);
            }

            return View(r);
        }
        public ActionResult DetailEmployee( int empId)
        {
            var em = (from e in _db.TUANANH__EMPLOYEE
                      join p in _db.TUANANH__POSITION on e.POS_ID equals p.POS_ID
                      where e.EMP_ID == empId
                      select new StaffInfo
                                 {
                                     EmpId = e.EMP_ID,
                                     EmpName = e.EMP_NAME,
                                     EmpAge = e.EMP_AGE,
                                     EmpInfo = e.EMP_INFO,
                                     EmpImage = e.EMP_IMAGE,
                                     EmpTitle = e.EMP_TITLE,
                                     PosId = e.POS_ID,
                                     PosDes = p.POS_DES,
                                     LeaderId = p.LEADER_ID
                                 }).SingleOrDefault();
            return View(em);
        }

    }
}
